package com.heifeng.cloudshelf;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.ConnectivityManager;
import android.text.TextUtils;
import android.util.Log;


import java.util.ArrayList;
import java.util.List;

public class NetWorkReceiver extends BroadcastReceiver {

    private static List<OnNetConnectListener> onNetConnectListeners = new ArrayList<>();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (TextUtils.equals(action, ConnectivityManager.CONNECTIVITY_ACTION)) {
            switch (isNetworkAvailable(context)) {
                case 1:
//                    ToastUitl.showLong("-----------networktest---------有线");
                    Log.e("NetWorkReceiver", "-----------networktest---------有线");
                    sendNetBroadcast(true);
                    break;
                case 2:
//                    ToastUitl.showLong("-----------networktest---------无线");
                    Log.e("NetWorkReceiver", "-----------networktest---------无线");
                    sendNetBroadcast(true);
                    break;
                case 0:
//                    ToastUitl.showLong("-----------networktest---------无网络");
                    Log.e("NetWorkReceiver", "-----------networktest---------无网络");
                    sendNetBroadcast(false);
                    break;
                default:
                    break;
            }
        }
    }

    private void sendNetBroadcast(boolean connect) {
        if (null != onNetConnectListeners && onNetConnectListeners.size() > 0) {
            for (int i = 0; i < onNetConnectListeners.size(); i++) {
                OnNetConnectListener onNetConnectListener = onNetConnectListeners.get(i);
                if (null == onNetConnectListener) {
                    onNetConnectListeners.remove(i);
                } else {
                    onNetConnectListener.isConnected(connect);
                }
            }
        }
    }


    public static void setOnNetConnectListener(OnNetConnectListener onNetConnectListener) {
        if (onNetConnectListeners == null) onNetConnectListeners = new ArrayList<>();
        onNetConnectListeners.add(onNetConnectListener);
    }

    private int isNetworkAvailable(Context context) {
        ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectMgr == null) return -1;
        NetworkInfo ethNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
        NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (ethNetInfo != null && ethNetInfo.isConnected()) {
            return 1;
        } else if (wifiNetInfo != null && wifiNetInfo.isConnected()) {
            return 2;
        } else {
            return 0;
        }
    }

    public static void clearNetConnectListeners() {
        if (onNetConnectListeners != null) {
            onNetConnectListeners.clear();
            onNetConnectListeners = null;
        }
    }

    public interface OnNetConnectListener {
        void isConnected(boolean isConnected);
    }
}

